package com.lry.basic.jvm.asm.generate_class;


import aj.org.objectweb.asm.ClassWriter;

import static aj.org.objectweb.asm.Opcodes.*;

public class MyClassLoader extends ClassLoader{

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        if(name.endsWith("_My")){
            ClassWriter cw = new ClassWriter(0);
            //版本，访问限定，接口，泛型？，父类，父接口
            cw.visit(V1_8,ACC_PUBLIC+ACC_ABSTRACT+ACC_INTERFACE,
                    name,//"com/lry/basic/jvm/asm/generate_class/Comparable"
                    null,"java/lang/Object",
                    new String[]{"com/lry/basic/jvm/asm/generate_class/Measurable"});

            cw.visitField(ACC_PUBLIC+ACC_FINAL+ACC_STATIC,
                    "LESS","I",null,new Integer(-1)).visitEnd();

            cw.visitField(ACC_PUBLIC+ACC_FINAL+ACC_STATIC,
                    "EQUAL","I",null,new Integer(0)).visitEnd();

            cw.visitField(ACC_PUBLIC+ACC_FINAL+ACC_STATIC,
                    "GREATER","I",null,new Integer(1)).visitEnd();

            cw.visitMethod(ACC_PUBLIC+ACC_ABSTRACT,"compareTo","(Ljava/lang/Object;)I",null,null).visitEnd();

            cw.visitEnd();

            byte[] b = cw.toByteArray();
            return defineClass(name,b,0,b.length);
        }

        return super.findClass(name);
    }
}
